package com.heartbeat.spider.impl;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.heartbeat.domain.Content;
import com.heartbeat.domain.Page;
import com.heartbeat.domain.Site;
import com.heartbeat.spider.ContentDao;
import com.heartbeat.spider.ContentService;
import com.heartbeat.spider.PageDao;
import com.heartbeat.spider.SiteDao;
import com.heartbeat.util.ImageUtil;
import com.heartbeat.util.Parser;

public class ContentServiceImpl implements ContentService {
	private static final String FILE_PATH = "E:/ny369_1";
	
	private Parser parser = new Parser();
	
	private SiteDao siteDao  = new SiteDaoImpl();
	
	private ImageUtil imageUtil = new ImageUtil();
	
	private ContentDao contentDao = new ContentDaoImpl();
	
	private PageDao pageDao =  new PageDaoImpl();
	@Override
	public void spideContent(Page page) {
		// TODO Auto-generated method stub
		Site site = siteDao.getSiteById(page.getSiteId());
		
		Document doc = parser.getDocument(page.getUrl(), site.getCodeType());
		if(doc == null){
			return;
		}
		//如果成功抓取网页
		NodeList nodeList = parser.getNodeList(doc, site.getRegularEx());
		int nodeListLength = nodeList.getLength();
		Node node = null;
		if(nodeListLength == 1){
			node = nodeList.item(0);
			if(site.getType() == 0){//如果抓取类型为图片
				Node srcNode = node.getAttributes().getNamedItem("src");
				if(srcNode != null){
					//System.out.println(imgNode.getNodeValue());
					String imgSrc = srcNode.getNodeValue();
					//获取图片后缀
					String suffix = imgSrc.substring(imgSrc.lastIndexOf(".")+1);
					//下载图片
					boolean spideImgSucced = imageUtil.spideImage(imgSrc, FILE_PATH, page.getSiteId()+"_" + page.getId()+"." +suffix);
					if(!spideImgSucced){
						return;
					}
					//保存内容信息
					String title="";
					Node altNode = node.getAttributes().getNamedItem("alt");
					if(altNode!=null && StringUtils.isNotEmpty(altNode.getNodeValue())){
						title = altNode.getNodeValue();
						System.out.println("alt:" + title);
					}else{
						title=this.getPageTitle(doc);
						
						System.out.println("title:"+title);
					}
					Content content = new Content();
					content.setPageId(page.getId());
					content.setPath(FILE_PATH+"/" + page.getSiteId()+"_" + page.getId()+"." +suffix);
					content.setSiteId(page.getSiteId());
					content.setTitle(title);
					content.setType(site.getType());
					contentDao.saveContent(content);
					
					//更新网页状态
					pageDao.setHasSpidedContent(page);
				}
				
			}
		}else{//如果没有符合规则的内容
			pageDao.setHasSpidedContent(page);
		}
		
		
	}
	
	public void test(String webUrl, String codeType){
		Document doc = parser.getDocument(webUrl, codeType);
		//System.out.println(doc.getTextContent());
		
		if(doc == null){
			return;
		}
		//如果成功抓取网页
		String title=this.getPageTitle(doc);
		System.out.println("==========" + title);
	}
	
	/**
	 *网页获取标题
	 * @param doc
	 * @return
	 */
	private String getPageTitle(Document doc){
		String title = "";
		
		NodeList titleNodeList = parser.getNodeList(doc,"//TITLE");
		if(titleNodeList.getLength()==1){
			Node titleNode = titleNodeList.item(0);
			title = titleNode.getTextContent();
			/*try {
				title = new String(title.getBytes("Windows-1252"),"utf-8");
				//title = new String(title.getBytes("gb2312"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
			//title = this.dowith(titleNode.getTextContent());
		}
		
		return title;
	}
	
	private String dowith(String s) {

		byte[] b = s.getBytes();
		if (b.length > 0) {
			for (int i = 0; i < b.length; i++) {
				if ((int) b[i] == 63) {
					b[i] = (byte) '\u0020';
				}
			}
		}
		return new String(b).trim();
	}
	
	public static void main(String args[]){
		PageDao pageDao = new PageDaoImpl();
		for(int i=0;i<10000; i++){
			List<Page> pageList = pageDao.getToSpideContentPages(0, 100);
			
			ContentService contentService = new ContentServiceImpl();
			
			for(Page page : pageList){
				contentService.spideContent(page);
			}
			
		}
		
	/*	ContentServiceImpl contentService = new ContentServiceImpl();
		contentService.test("http://www.163.com/", "gb2312");*/
	}

}
